<template>
	<view>
		<view class="form-view">
			
			<view class="form-item">
				<uni-icons type="person-filled" size="25"></uni-icons>
				<input placeholder="紧急联系人姓名" v-model="form.contactsName"/>
			</view>
			<view class="form-item">
				<uni-icons type="phone-filled" size="25"></uni-icons>
				<input placeholder="紧急联系人手机号" maxlength="11" v-model="form.contactsTel"/>
			</view>
			<view class="tip-view">建议填写真实姓名，方便沟通</view>
			<view class="save-btn" @click="save">保存</view>
		</view>
	</view>
</template>

<script>
	import * as wxUserContacts from '../../common/api/wxUserContacts.js'
	export default {
		data() {
			return {
				uid:'',
				editStatus:'add',
				form:{
					phoneNum:'',
					contactsName:'',
					contactsTel:'',
					defaultContacts:'否',
				}
			}
		},
		onLoad(opt) {
			if(opt.uid){
				this.uid = opt.uid
				this.editStatus = 'edit'
				uni.setNavigationBarTitle({
					title:'编辑紧急联系人'
				})
				this.getData()
			}else{
				this.initPhoneNum()
			}
		},
		methods: {
			initPhoneNum(){
				const dataJson = uni.getStorageSync('user_info') || '{}'
				console.log('user_info dataJson',dataJson);
				const data = JSON.parse(dataJson) || {}
				this.form.phoneNum = data.tel
			},
			getData(){
				const param = {
					uid:this.uid
				}
				wxUserContacts.queryById(param).then(result => {
					this.form = result.data.data
				}).catch(error => {
					console.log(error);
				})
			},
			check(){
				console.log('check',JSON.stringify(this.form));
				if(!this.form.contactsName){
					uni.showToast({
						title: '紧急联系人姓名不能为空',
						icon:'none'
					});
					return false
				}
				if(!this.form.contactsTel){
					uni.showToast({
						title: '紧急联系人手机号',
						icon:'none'
					});
					return false
				}
				return true
			},
			save(){
				if(!this.check()){
					return
				}
				if(this.editStatus === 'add'){
					this.add()
				}else{
					this.edit()
				}
			},
			add(){
				const param = Object.assign({},this.form)
				wxUserContacts.insert(param).then(result => {
					uni.showToast({
						title:result.data.msg
					})
					uni.navigateBack()
				}).catch(error => {
					console.log(error);
				})
			},
			edit(){
				const param = Object.assign({},this.form,{
					createTime:undefined,
					updateTime:undefined,
				})
				wxUserContacts.update(param).then(result => {
					uni.showToast({
						title:result.data.msg
					})
					uni.navigateBack()
				}).catch(error => {
					console.log(error);
				})
			}
		}
	}
</script>
<style>
	page{
		background: white;
	}
</style>
<style lang="scss" scoped>
.form-view{
	padding: 30rpx;
	box-sizing: border-box;
	.form-item{
		width: 100%;
		min-height: 100rpx;
		box-sizing:border-box;
		padding: 10rpx;
		background: #F5F6F8;
		display: flex;
		align-items: center;
		border-radius: 10rpx;
		margin-top: 30rpx;
		input{
			flex: 1;
			margin-left: 20rpx;
			margin-right: 20rpx;
		}
	}
	.tip-view{
		margin-top: 10rpx;
		font-size: 14rpx;
	}
	.save-btn{
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 50rpx;
		background: #169BD5;
		color: white;
		border-radius: 10rpx;
	}
	
}
</style>
